查看原文
其他

震惊小伙伴的单行Python代码

2016-07-12 编程派

编程派微信号:codingpy


在昨天分享的《请不要为了炫耀而写出超短代码》一文中,提到了两种短代码:一种是写给自己看的,另一种是写给别人看的。本文再分享一些短代码的示例,有些虽短,但也有可读性;有些就纯粹属于写给自己看的了。


1、让列表中的每个元素都乘以2

print map(lambda x: x * 2, range(1,11))

2、求列表中的所有元素之和

print sum(range(1,1001))

3、判断一个字符串中是否存在某些词

wordlist = ["scala", "akka", "play framework", "sbt", "typesafe"] tweet = "This is an example tweet talking about scala and sbt."

print map(lambda x: x in tweet.split(),wordlist)

4、读取文件

print open("ten_one_liners.py").readlines()

5、祝你生日快乐!

print map(lambda x: "Happy Birthday to " + ("you" if x != 2 else "dear Name"),range(4))

6. 过滤列表中的数值

print reduce(lambda(a,b),c: (a+[c],b) if c > 60 else (a,b + [c]), [49, 58, 76, 82, 88, 90],([],[]))

7. 获取XML web service数据并分析

from xml.dom.minidom import parse, parseString
import urllib2
# 注意,我将它转换成XML格式化并打印出来

print parse(urllib2.urlopen("http://search.twitter.com/search.atom?&q=python")).toprettyxml(encoding="utf-8")


8. 找到列表中最小或最大的一个数字

print min([14, 35, -7, 46, 98])
print max([14, 35, -7, 46, 98])

9. 并行处理

import multiprocessing
import math
print list(multiprocessing.Pool(processes=4).map(math.exp,range(1,11)))

10. “Sieve of Eratosthenes”算法


Python里没有埃拉托色尼选筛法(Sieve of Eratosthenes)操作符,但这对于Python来说并不是难事。

n = 50 # 求 2-50 之间的素数
print sorted(set(range(2,n+1)).difference(set((p * f) for p in range(2,int(n**0.5) + 2) for f in range(2,(n/p)+1))))


【近期热文推荐】

程序猿都会爬的妹子图

《Think Python 2e》中译版电子书分享

Pythonista:移动端的Python好帮手

Anaconda:最佳的Python个人应用解决方案

看完这些 你的“程序员指数”或将飙升

Python制作安卓游戏外挂

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存